<template>
    <div class="add">
        <el-form label-position="left" label-width="80px" :rules="rules" ref="formLabelAlign" :model="formLabelAlign">
            <el-form-item label="分类" prop="typeId">
                <el-select v-model="formLabelAlign.typeId" placeholder="请选择分类">
                    <el-option v-for="item in typeList" :label="item.typeName" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item style="margin-top: 55px" label="标题" prop="title">
                <el-input v-model="formLabelAlign.title" placeholder="请输入标题"></el-input>
            </el-form-item>
            <el-form-item style="margin-top: 55px" label="内容" prop="content">
                <Editor v-model="formLabelAlign.content" />
            </el-form-item>
        </el-form>
        <el-button @click="release('formLabelAlign')">发布</el-button>
    </div>
</template>

<script>
    import Editor from "../components/Editor";
    export default {
        name: "AddInformation",
        components: {
            Editor
        },
        computed: {},
        data() {
            return {
                typeList: [],
                formLabelAlign: {
                    title: '',
                    typeId: '',
                    content: ''
                },
                rules: {
                    title: [
                        {required: true, message: '请输入标题', trigger: 'blur'}
                    ],
                    typeId: [
                        {required: true, message: '请输入选择类别', trigger: 'blur'}
                    ],
                    content: [
                        {required: true, message: '请输入内容', trigger: 'blur'}
                    ]
                },
            }
        },
        created() {
            this.formLabelAlign = {
                title: '',
                typeId: '',
                content: ''
            }
            this.$axios.get("/api/type/getAll", {})
                .then(res => {
                    this.typeList = res.data.data
                }).catch(err => {
                return Promise.reject(err);
            })
        },
        methods: {
            release(formName) {
                this.$refs[formName].validate(valid => {
                    if (valid) {
                        this.$axios.post("/api/information/insert", this.$qs.stringify(this.formLabelAlign))
                            .then(res => {
                                if (res.data.status == 200) {
                                    this.$message.success(res.data.message)
                                    this.$emit('update:isAdd', false)
                                } else {
                                    this.$message.error(res.data.message)
                                }
                            })
                            .catch(err => {
                                return Promise.reject(err);
                            })
                    } else {
                        return false;
                    }
                });
            }
        }
    }
</script>

<style>
    .add {
        width: 800px;
    }

    .ql-editor {
        height: 360px;
    }
</style>
